package com.mohan.gulimall.product.controller;

import java.net.InetAddress;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.mohan.gulimall.product.entity.CategoryEntity;
import com.mohan.gulimall.product.service.CategoryService;
import com.mohan.common.utils.PageUtils;
import com.mohan.common.utils.R;


/**
 * 商品三级分类
 *
 * @author mohan
 * @email 1358665425@qq.com
 * @date 2021-12-11 09:47:42
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
  @Autowired
  private CategoryService categoryService;

  /**
   * 列表
   */
  @RequestMapping("/list/tree")
  //@RequiresPermissions("product:category:list")
  public R list() {
    List<CategoryEntity> list= categoryService.getAllTreeCategory();
    return R.ok().put("data", list);
  }


  /**
   * 信息
   */
  @RequestMapping("/info/{catId}")
  //@RequiresPermissions("product:category:info")
  public R info(@PathVariable("catId") Long catId) {
    CategoryEntity category = categoryService.getById(catId);

    return R.ok().put("category", category);
  }

  /**
   * 保存
   */
  @RequestMapping("/save")
  //@RequiresPermissions("product:category:save")
  public R save(@RequestBody CategoryEntity category) {
    categoryService.save(category);

    return R.ok();
  }

  /**
   * 批量修改
   */
  @RequestMapping("/update/sort")
  public R updateSort(@RequestBody List<CategoryEntity> categoryEntities) {
    try{
      InetAddress addr = InetAddress.getLocalHost();
      System.out.println("IP地址：" + addr.getHostAddress() + "，主机名：" + addr.getHostName());
    }catch (Exception e) {

    }
    System.out.println(categoryEntities);
    boolean b = categoryService.updateBatchById(categoryEntities);
    return R.ok();
  }

  /**
   * 修改
   */
  @RequestMapping("/update")
  //@RequiresPermissions("product:category:update")
  public R update(@RequestBody CategoryEntity category) {
//    categoryService.updateById(category);
    categoryService.updateDetail(category);
    return R.ok();
  }

  /**
   * 删除
   * @RequestBody: 获取请求体，只有post才会发送post请求
   */
  @RequestMapping("/delete")
  //@RequiresPermissions("product:category:delete")
  public R delete(@RequestBody Long[] catIds) {
    //1.检查当前删除菜单是否被别的地方引用
    categoryService.removeMenuByIds(Arrays.asList(catIds));

    return R.ok();
  }

}
